package org.agilelang.fgl.parser;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import org.agilelang.object.Project;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Parser {

    ApplicationProcess application;
    ModelProcess model;
    ControllerProcess controller;
    MenuProcess menu;
    ReportProcess report;
    WebServicesProcess webservices;
    WebClientProcess webclient;
    String error;

    public Parser() {
        application = new ApplicationProcess();
        model = new ModelProcess();
        controller = new ControllerProcess();
        webservices = new WebServicesProcess();
        webclient = new WebClientProcess();
        menu = new MenuProcess();
        report = new ReportProcess();

    }

    public void process(HashMap lines) {
        Iterator iterator = lines.keySet().iterator();

        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            processLine(lines.get(key).toString(), key);
        }
    }

    public void processLine(String aLine, String line) {
        String token = Util.getTypeObject(aLine);
        switch (token) {
            case ReservedWord.APPLICATION:
                application.process(aLine);
                break;
            case ReservedWord.MODEL:
                if (!model.process(aLine)) {
                    error = line + " --> " + model.getError();
                }
                break;
            case ReservedWord.CONTROLLER:
                if (!controller.process(aLine)) {
                    error = line + " --> " + controller.getError();
                }
                break;
            case ReservedWord.MENU:
                menu.process(aLine);
                break;
            case ReservedWord.REPORT:
                report.process(aLine);
                break;
            case ReservedWord.WEBSERVICES:
                webservices.process(aLine);
                break;
            case ReservedWord.WEBCLIENT:
                webclient.process(aLine);
                break;
            default:
                break;
        }

    }

    public String getXml() {
        Project project = new Project();

        project.application = application.getApplication();
        project.menus = menu.getListMenu();
        project.models = model.getListModel();
        project.controllers = controller.getListController();
        project.webservices = webservices.getListWebServices();
        project.webclient = webclient.getListWebClient();
        project.reports = report.getListReport();


        JAXBContext jc = null;
        Marshaller marshaller;
        java.io.StringWriter sw = new StringWriter();
        try {
            jc = JAXBContext.newInstance(Project.class);
            marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(project, sw);

        } catch (JAXBException ex) {
            error = "Error --> " + ex.toString();
        }

        return sw.toString();
    }

    public String getError() {
        return error;
    }
}
